package com.example.lirisheng.springbootfinalexperiment.repositories;



import com.example.lirisheng.springbootfinalexperiment.entity.Users;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.transaction.annotation.Transactional;

public interface UsersRepository extends CrudRepository<Users,Integer> {


    @Modifying
    @Query(value = " insert into  users(username,password,open_id) values (?1,?2,?3) ",nativeQuery = true)
     void  mySave(String username, String password, String opendId);

     Users findByUsername(String username);

     Users findByOpenId(String openId);

//     Users findById(Integer resId);

    Users findByUsernameAndPassword(String username,String password);

    Users findByIdAndPassword(Integer resId,String password);

    @Modifying(clearAutomatically = true)
    @Query("update  Users set password=?1  where  id=?2 ")
    public void UpdatePasswordById(String password, Integer resId);




    /**
     * 注意:如果nativeQuery=true,则不适用对象映射,即可使用表名
     * 但nativeQuety=false,则使用对象映射,则需用DOMAIN类名
     * https://blog.csdn.net/AAAblues/article/details/83303172?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-4.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-4.nonecase
     * @param openId
     * @param username
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Modifying(clearAutomatically = true)
    @Query("update  Users set open_id=?1  where  username=?2 ")
     public int UpdateOpenIdByUsername(String openId, String username);

    @Transactional(rollbackFor = Exception.class)
    @Modifying(clearAutomatically = true)
    @Query("update  Users set enabled=?1  where  resId=?2 ")
    public int UpdateEnableByResId(Integer enable,Long id);


}
